package com.tywj.panda.customer.common.tool

import android.app.Activity
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.callbacks.onCancel
import com.blankj.utilcode.util.PermissionUtils
import com.tywj.panda.customer.common.R
import com.tywj.panda.customer.common.view.dialog.showByCZConfig
import com.tywj.lib.core.common.util.AppUtil
import com.tywj.lib.core.common.util.ToastUtil
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/18
 *     desc   : 权限工具类
 *     version: 1.0.0
 * </pre>
 */
object PermissionTool {


    /**
     * 请求打开摄像头权限
     * 请求失败显示toast
     */
    fun camera(action: () -> Unit) {
        AndPermission.with(AppUtil.context)
            .runtime()
            .permission(Permission.CAMERA, Permission.READ_EXTERNAL_STORAGE)
            .onGranted {
                action.invoke()
            }
            .onDenied {
                ToastUtil.shortToast(R.string.common_permission_denied_prompt_camera)
            }
            .start()
    }

    /**
     * 请求存储权限
     * 请求失败显示toast
     */
    fun storage(action: () -> Unit) {
        AndPermission.with(AppUtil.context)
            .runtime()
            .permission(Permission.Group.STORAGE)
            .onGranted {
                action.invoke()
            }
            .onDenied {
                ToastUtil.shortToast(R.string.common_permission_denied_prompt_external_storage)
            }
            .start()
    }

    /**
     * 请求定位权限
     * 请求失败显示toast
     */
    fun location(successAction: () -> Unit) {
        location(successAction) {
            showOpenLocationToast()
        }
    }

    /**
     * 请求定位权限
     * 请求失败显示弹窗
     */
    fun location(activity: Activity, successAction: () -> Unit) {
        location(successAction) {
            showOpenLocationDialog(activity) {}
        }
    }

    fun location(successAction: () -> Unit, failAction: () -> Unit) {
        AndPermission.with(AppUtil.context)
            .runtime()
            .permission(Permission.Group.LOCATION)
            .onGranted {
                successAction.invoke()
            }
            .onDenied {
                failAction.invoke()
            }
            .start()
    }

    fun showOpenLocationDialog(activity: Activity, failAction: () -> Unit) {
        MaterialDialog(activity)
            .title(R.string.common_permission_denied_prompt_location_title)
            .message(R.string.common_permission_denied_prompt_location_message)
            .negativeButton(R.string.common_negative) {
                failAction.invoke()
            }
            .positiveButton(R.string.common_permission_denied_prompt_positive) {
                PermissionUtils.launchAppDetailsSettings()
            }
            .onCancel { failAction.invoke() }
            .showByCZConfig()
    }

    fun showOpenLocationToast() {
        ToastUtil.shortToast(R.string.common_open_location_permission)
    }

}